<?php
namespace ILab;

use Exception;

require_once __DIR__ . '/HttpCurl.php';

class ILab
{
    public $appID;
    public $secret ;
    public $server = 'http://www.ilab-x.com';
    // public $server = 'http://202.205.145.156:8017';
    public $tokenUrl = '/open/api/v2/token';
    public $refreshUrl = '/open/api/v2/token/refresh';
    public $dataUploadUrl = '/open/api/v2/data_upload';
    public $attachmentUploadUrl = '/open/api/v2/attachment_upload';

    /**
     * * * * * * * * * * * * * * * * *
     * ILab constructor.
     * @param string $appID
     * @param string $secret
     * @throws Exception
     * * * * * * * * * * * * * * * * *
     */
    public function __construct($appID = '' , $secret = '')
    {
        $this->validateAppID($appID);
        $this->validateSecret($secret);
        $this->appID = $appID;
        $this->secret = $secret;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes: ILab constructor get users and access_token function.
     * @param $ticket
     * @return Client
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function token($ticket)
    {
        $url = $this->makeAccessTokenUrl($ticket, $this->makeSignature($ticket));
        $httpClient = new \Curl\HttpCurl();
        return $httpClient->get($url);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:ILab constructor refresh access_token function.
     * @user: Bennent_G
     * @param $token
     * @return bool|mixed|string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function refresh($token)
    {
        $url = $this->makeRefreshTokenUrl($token, $this->makeSignature($token, 'refresh'));
        $httpClient = new \Curl\HttpCurl();
        return $httpClient->get($url);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:ILab constructor data_upload function.
     * @user: Bennent_G
     * @param $token
     * @param $data
     * @return bool|mixed|string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function data_upload($token, $data)
    {
        $url = $this->makeUploadUrl($token);
        $httpClient = new \Curl\HttpCurl();
        return $httpClient->jsonPost($url, $data);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:ILab constructor attachment_upload function.
     * @user: Bennent_G
     * @param $token
     * @param $originId
     * @param $fileName
     * @param $title
     * @param null $remarks
     * @param $filePath
     * @return bool|mixed|string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function attachment_upload($token, $originId, $fileName, $title, $remarks = null, $filePath)
    {
        $url = $this->makeAttachmentUploadUrl($token, $originId, $fileName, $title, $remarks);
        $httpClient = new \Curl\HttpCurl();
        return $httpClient->post($url, 'json', $filePath);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * ILab constructor validate.
     * @notes:validate params
     * @param string $appID
     * @throws Exception
     * * * * * * * * * * * * * * * * *
     */
    private function validateAppID(string $appID)
    {
        if (!$appID || is_null($appID)) {
            throw new Exception('params error');
        }
    }

    /**
     * * * * * * * * * * * * * * * * *
     * ILab constructor validate.
     * @notes:validate params
     * @param string $secret
     * @throws Exception
     * * * * * * * * * * * * * * * * *
     */
    private function validateSecret(string $secret)
    {
        if (!$secret || is_null($secret)) {
            throw new Exception('params error');
        }
    }

    /**
     * * * * * * * * * * * * * * * * *
     * ILab make access_token request url.
     * @user: Bennent_G
     * @param $ticket
     * @param $signature
     * @return string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    private function makeAccessTokenUrl($ticket, $signature)
    {
        return $this->server . $this->tokenUrl . '?ticket=' . $ticket . '&appid=' . $this->appID . '&signature=' . $signature;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes: ILab make access_token_refresh request url.
     * @user: Bennent_G
     * @param $token
     * @param $signature
     * @return string
     * @throws
     * @remark access_token must be encode ！！！
     * * * * * * * * * * * * * * * * *
     */
    private function makeRefreshTokenUrl($token, $signature)
    {
        return $this->server . $this->refreshUrl . '?access_token=' . urlencode($token) . '&appid=' . $this->appID . '&signature=' . $signature;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:ILab make data upload request url.
     * @user: Bennent_G
     * @param $token
     * @return string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    private function makeUploadUrl($token)
    {
        return $this->server . $this->dataUploadUrl . '?access_token=' . urlencode($token);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:ILab make attachment upload request url.
     * @user: Bennent_G
     * @param $token
     * @param $originId
     * @param $fileName
     * @param $title
     * @param $remarks
     * @return string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    private function makeAttachmentUploadUrl($token, $originId, $fileName, $title, $remarks)
    {
        $url =  $this->server . $this->attachmentUploadUrl . '?access_token=' . urlencode($token) . '&appid=' . $this->appID . '&originId=' . $originId . '&filename=' . urlencode($fileName) . '&title=' . urlencode($title);
        if(!is_null($remarks)) $url .= '&remarks=' . urlencode($remarks);
        return $url;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes: ILab make signature.
     * @user: Bennent_G
     * @param $args
     * @param string $type
     * @return string
     * @throws
     * type : 默认ticket; token
     * * * * * * * * * * * * * * * * *
     */
    private function makeSignature($args, $type = 'ticket')
    {
        $prefix =  $type == 'ticket' ? $this->decodeTicket($args) : $args;
        return strtoupper(md5( $prefix . $this->appID . $this->secret));
    }

    private function getFileContent($filePath)
    {
        return file_get_contents($filePath);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * ILab decode ticket.
     * @param $ticket
     * @return string
     * * * * * * * * * * * * * * * * *
     */
    private function decodeTicket($ticket) { return urldecode($ticket); }

    public function __call($method, $args) { throw new Exception("$method is not found"); }

    public function __clone() {}
}

